package com.android.questions.ui.adapter;

import android.annotation.SuppressLint;
import android.content.Context;
import android.text.Html;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.android.questions.R;
import com.android.questions.config.AppConfig;
import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.hx.currency.data.qa.model.QAWithdraw;

/**
 * FileName: BalanceAdapter
 * Author: admin
 * Date: 2021/11/4 15:07
 * Description:
 */
public class BalanceAdapter extends BaseQuickAdapter<QAWithdraw, BaseViewHolder> {

    private OnItemSelectLister itemSelectLister;
    private volatile int selectPos = 0;       //默认选中第一个

    public BalanceAdapter() {
        super(R.layout.item_balance);
    }

    @Override
    protected int getDefItemViewType(int position) {
        return position;
    }

    @SuppressLint("SetTextI18n")
    @Override
    protected void convert(final BaseViewHolder baseViewHolder, QAWithdraw qaWithdraw) {
        Context context = getContext();
        int position = getItemPosition(qaWithdraw);

        ConstraintLayout itemLayout = baseViewHolder.getView(R.id.itemLayout);
        TextView tvRmb = baseViewHolder.getView(R.id.tvRmb);
        TextView tvTag = baseViewHolder.getView(R.id.tvTag);
        TextView tvWith = baseViewHolder.getView(R.id.tvWith);
        ImageView ivBg = baseViewHolder.getView(R.id.iv_bg);
        ImageView imgSzdjdh = baseViewHolder.getView(R.id.imgSzdjdh);
        ImageView imgGuangQuan = baseViewHolder.getView(R.id.imgGuangQuan);
        TextView tvAdNum = baseViewHolder.getView(R.id.tvAdNum);

        if (qaWithdraw.getVt() == 1) {
            AppConfig.tiXianPos = position;
            tvAdNum.setVisibility(View.VISIBLE);
            if (qaWithdraw.getWv() == 0) {
                tvAdNum.setText("可立即提现");
            } else {
                if (position == selectPos) {
                    tvAdNum.setText(Html.fromHtml("再看<font color='#1eff00'>" + qaWithdraw.getWv() + "</font>个视频立即到账"));
                } else {
                    tvAdNum.setText(Html.fromHtml("再看<font color='#00fff6'>" + qaWithdraw.getWv() + "</font>个视频立即到账"));
                }
            }
        } else {
            tvAdNum.setVisibility(View.GONE);
        }

        imgSzdjdh.setVisibility(View.GONE);
        imgGuangQuan.setVisibility(View.GONE);

        if (position == 0 && AppConfig.getMeBalanceState() && qaWithdraw.getWls() != 2) {
            imgSzdjdh.setVisibility(View.VISIBLE);
            imgGuangQuan.setVisibility(View.VISIBLE);

            Glide.with(context)
                    .asGif()
                    .load(R.drawable.icon_szdjdh)
                    .into(imgSzdjdh);

            Glide.with(context)
                    .asGif()
                    .load(R.drawable.icon_guangquan)
                    .into(imgGuangQuan);
        }

        tvRmb.setText(qaWithdraw.getWa() + "元");

        if (position == selectPos) {
            ivBg.setImageResource(R.drawable.ic_balance_item_selected);
            tvAdNum.setBackgroundResource(R.drawable.bg_fragment_me_balance_select);
        } else {
            ivBg.setImageResource(R.drawable.ic_balance_item_unselect);
            tvAdNum.setBackgroundResource(R.drawable.bg_fragment_me_balance_unselect);
        }

        int remainTimes = qaWithdraw.getWc();
        if (remainTimes <= 0) {//提现次数已用完
            tvWith.setVisibility(View.VISIBLE);
            tvTag.setVisibility(View.GONE);
            itemLayout.setEnabled(false);
            ivBg.setImageResource(R.drawable.ic_balance_item_unselect);
        } else {
            if (remainTimes > 10) {
                tvTag.setText("不限次数");
            } else {
                tvTag.setText("今日剩余" + remainTimes + "次");
            }
            tvTag.setVisibility(View.VISIBLE);
            itemLayout.setEnabled(true);
        }

        itemLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                selectPos = position;
                notifyDataSetChanged();
                if (itemSelectLister != null) {
                    itemSelectLister.onSelect(selectPos);
                }
            }
        });
    }

    public void setItemSelectLister(OnItemSelectLister selectLister) {
        this.itemSelectLister = selectLister;
    }

    public interface OnItemSelectLister {
        void onSelect(int position);
    }
}